Skip to content

fix: skip already-encoded responses and drop stale Content-Length - #26

Merged
CodeDredd merged 1 commit into
mainfrom
fix/already-encoded-responses
Nov 24, 2026
Merged

CodeDredd merged 1 commit into
mainfrom
fix/already-encoded-responses

Conversation

@CodeDredd

Copy link
Copy Markdown
Owner

Closes #24

What

compress() and compressStream() — the functions behind useCompression*, the h3 v1 app hook and the Nuxt module — now behave like the h3 v2 middleware path already did:

guards Content-Encoding fixes Content-Length
compressResponse() / compressResponseStream() ✅ (unchanged) ✅ (unchanged)
compress() ✅ new ✅ new
compressStream() ✅ new ✅ new
  1. Already-encoded responses pass through untouched. If the response carries a Content-Encoding other than identity — e.g. a pre-built .br asset served by Nitro's static handler with compressPublicAssets: true — nothing is compressed, re-wrapped or re-headered. The check looks at the event's response headers and at the header record a render:response hook hands over (Nitro only applies that record after the hook ran).
  2. The stale Content-Length is dropped whenever we set Content-Encoding, again on both the event and the hook record. The runtime recomputes it (buffered path) or switches to chunked framing (stream path). This is a framing fix on its own, independent of (1).

h3 1.6 / 1.7 compatibility

removeResponseHeader only exists since h3 1.8, but the peer range starts at ^1.6.0. It is therefore read through the same runtime-key indirection as send / toResponse (so re-bundlers don't emit a static named import), with a node.res.removeHeader fallback. Verified manually against h3 1.7.1; the downstream-bundling test stub now also omits removeResponseHeader and asserts no missing-export warning for it.

Tests

New test/already-encoded.test.ts:

  • h3 v2 (mockEvent): compress, compressStream, useCompression, useCompressionStream and the Nuxt plugin leave a pre-compressed asset untouched; Content-Length is removed from the event and from the render:response record when compressing and kept when nothing is compressed; Content-Encoding: identity still compresses; both middlewares pass an encoded Response through.
  • h3 v1 (real server): the reproduction from the issue, read over a raw socket — fetch/superagent decode one layer and would hide the bug. Asserts the announced Content-Length equals the bytes on the wire and that a single decode yields the original.

All 15 new assertions-bearing tests fail on main and pass here, on both h3 v1 (1.8.1) and v2.

`compress()` and `compressStream()` compressed responses that already carry a
`Content-Encoding` (e.g. pre-built `.br` assets served by Nitro's static
handler with `compressPublicAssets`) a second time, and kept the
`Content-Length` of the uncompressed body. Browsers then decoded only one
layer and the announced length no longer matched the bytes on the wire.

Both now bail out on an existing (non-identity) `Content-Encoding` — checked
on the event and on the `render:response` header record — and remove the
stale `Content-Length` whenever they set one, mirroring what the h3 v2
middleware path already did.

`removeResponseHeader` only exists since h3 1.8, so it is read through the
runtime key like `send` / `toResponse`, with a `node.res` fallback.

Closes #24
@CodeDredd
CodeDredd merged commit 42995d8 into main Nov 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

compress()/compressStream() double-compress already-encoded responses and leave a stale Content-Length

1 participant